package by.alex.dzeshko.facebooksuperclient;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import by.alex.dzeshko.facebooksuperclient.async.common.ParamCallback;
import by.alex.dzeshko.facebooksuperclient.constants.FbConstants;
import by.alex.dzeshko.facebooksuperclient.image.ImageLoader;
import by.alex.dzeshko.facebooksuperclient.utils.FbPictureManager;
import by.alex.dzeshko.facebooksuperclient.utils.InetChecker;


public class ImageActivity extends FragmentActivity {
	
	private ImageView mImageView;
	
	private ProgressBar mProgressView;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_image);
		mImageView = (ImageView) findViewById(R.id.ivMain);
		mProgressView = (ProgressBar) findViewById(R.id.pbLoadingContent);
		String url = getIntent().getStringExtra(FbConstants.URL);
		if(TextUtils.isEmpty(url)) {
			mProgressView.setVisibility(View.GONE);
			mImageView.setImageResource(android.R.drawable.ic_dialog_alert);
		} else{
			InetChecker.checkInetConnection(this);
			url = FbPictureManager.getLargePictureUrl(url);
			Log.d("ImageActivity","url for load: "+url);
			loadImage(url);
		}
	}

	private void loadImage(String url) {
		ImageLoader.getInstance().bind(mImageView, url, new ParamCallback<Void>() {
			
			@Override
			public void onSuccess(Void p) {
				mProgressView.setVisibility(View.GONE);
			}
			
			@Override
			public void onError(Throwable e) {
				mProgressView.setVisibility(View.GONE);
			}
		});
	}

}
